#include <iostream>
#include <fstream>
#include <sstream>
#include <iosfwd>
#include <cstdio>
#include <cstring>
#include <locale>
#include <cassert>
using namespace std;
void testReadLine()
{
    stringstream in("hello world!\n"
                    "hello kf\n");
    char line[100];
    in.getline(line,100);
    cout<<line<<endl;
    assert(string(line) == "hello world!");
}
void testReadString()
{
    stringstream in("hello world!\n"
                    "hello kf\n");

    string s;
    in>>s;
    assert(s=="hello");
    in.seekg(0,ios::beg);
    while(!in.eof()){
        in>>s;
        if(!in.eof())
            cout<<s<<" ";
    }
    cout<<endl;
}
void testReadMix()
{

    stringstream io("hello kf 10\n"
                    "hello world!");
    cin.rdbuf(io.rdbuf());
    string s;
    int i;
    cin>>s;
    assert(s == "hello");
    cin>>s;
    assert(s == "kf");
    cin>>i;
    assert(i == 10);
    char line[100];
    //cin.get();
    cin.ignore();
    cin.getline(line,100);
    assert(string(line) == "hello world!");
}
void testRedirect()
{
    stringstream io("hello kf 10");
    cin.rdbuf(io.rdbuf());
    string hello,name;
    cin>>hello>>name;
    cout<<hello<<" "<<name;
    int age;
    cin>>age;
    cout<<" "<<age<<endl;
    assert(age == 10);
}

#define D(x) cout<<#x<<endl;x
void testFmt()
{
    long long ll = 123456L;
    long double ld = 12455.12365;
    string s = "kuangfengxyy@gmail.com";
    //立即刷新缓冲
    D(cout.setf(ios::unitbuf);)
    D(cout.setf(ios::showbase);)
    D(cout.setf(ios::showpoint);)
    D(cout.setf(ios::showpos);)

    //大写
    D(cout.setf(ios::uppercase);)
    /**十六进制*/
    D(cout.setf(ios::hex,ios::basefield);)
    cout<<ll<<endl;
    /**八进制*/
    D(cout.setf(ios::oct,ios::basefield);)
    cout<<ll<<endl;
    /**十进制*/
    D(cout.setf(ios::dec,ios::basefield);)
    cout<<ll<<endl;

    cout<<endl;
    cout<<ld<<endl;
    D(cout.setf(ios::scientific,ios::floatfield);)
    cout<<ld<<endl;
    D(cout.setf(ios::fixed,ios::floatfield);)
    cout<<ld<<endl;
    D(cout.unsetf(ios::floatfield);)
    D(cout.precision(4);)
    cout<<ld<<endl;

    cout<<endl;
    D(cout.fill('0');)
    cout<<"fill char :"<<cout.fill()<<endl;
    D(cout.width(30);)
    cout<<ll<<endl;
    D(cout.setf(ios::right,ios::adjustfield);)
    D(cout.width(30);)
    cout<<ll<<endl;
    D(cout.setf(ios::internal,ios::adjustfield);)
    D(cout.width(30);)
    cout<<ll<<endl;

}
void testCFmt()
{
    char inbuf[]="hello kf : 10\n" "hello world!";
    char hello[10],name[10],buf[100];
    int age;
    sscanf(inbuf,"%s %s :%d",hello,name,&age);
    sprintf(buf, "%s %s : %d",hello,name,age);
    assert(string(buf) == "hello kf : 10");

    char *token = strtok(inbuf,"\n");
    while(token != NULL)
    {
        /**因为strtok会将\n转为\0，所以这里可以直接输出*/
        printf("%s\n",token);

        token = strtok(NULL,"\n");
    }
    printf("%s\n",inbuf);
}
int main()
{
    //testReadLine();
    //testReadString();
    //testRedirect();
    //testFmt();
    //testReadMix();
    testCFmt();
}
